package com.zyz.forum.controller;

import com.zyz.forum.common.AppResult;
import com.zyz.forum.common.ResultCode;
import com.zyz.forum.model.Board;
import com.zyz.forum.service.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "板块接口")
@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {

    @Value("${my-forum.board.num:9}")
    private Integer num;
    @Resource
    private IBoardService boardService;

    @ApiOperation("获取首页板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        List<Board> boardList= boardService.selectBaseByNum(num);
        // 判断是否为空
        if (boardList == null) {
            boardList = new ArrayList<>();
        }
        // 返回结果
        return AppResult.success(boardList);
    }

    @ApiOperation("获取板块信息")
    @GetMapping("/getBoardInfoById")
    public AppResult<Board> getBoardInfoById(@ApiParam("板块id") @RequestParam("id") @NonNull Long id) {
        // 调用service
        Board board = boardService.selectById(id);
        if (board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS);
        }
        // 返回结果
        return AppResult.success(board);
    }
}
